package controller;

import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import service.UserService;

import java.util.List;

/**
 * Created by Tiler on 2020/11/24
 */
//控制层注解：用于处理用户的输入输出
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("index")
    public String index() {
        return "user_login";
    }

    //输入时，表单组件的名称与接收参数名称保持一致
    @RequestMapping("user_login")
    public String userLogin(String username, String password) {
        System.out.println("username: " + username);
        System.out.println("password: " + password);

        return "index";
    }

    //请求映射：将请求与函数进行绑定
    //model：用于存放输出数据，是对request的封装
    @RequestMapping("all_users")
    public String getAllUsers(Model model) {
        List<User> users = userService.getAllUsers();

        //将输出数据放入model中
        model.addAttribute("users", users);

        //返回值加上视图解析器中定义的前后缀形成完整的页面路径
        return "all_users";  //完整路径为：/jsp/index.jsp
    }
}
